package rbac.service;

import rbac.domain.dto.UserDto;
import rbac.domain.entity.Dept;
import rbac.domain.entity.Role;
import rbac.domain.entity.User;
import rbac.domain.vo.Result;

public class UserService {
    public static int DATA_SIZE = 100;
    public static Dept[] data = new Dept[DATA_SIZE+1];
    public static int idCounter = 1;

    public static User createEntity(UserDto dto, int id){
        User user = new User(dto.username, dto.name, dto.mobile,dto.mobile, dto.deptId,dto.roleId);
        user.id = id;
        return user;
    }

    public static void updateEntity(UserDto dto,User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }
    public static Result create(UserDto dto){
        if(idCounter > DATA_SIZE){
            return Result.failure("创建失败，超出储存容量");
        }
        if(!RoleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在该角色");
        }
        if(!DeptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在该部门");
        }

        User user = createEntity(dto, idCounter++);

        data[user.id] = user;

        return Result.success("创建成功",user);

    }
}
